fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn caculate_length(s: &String) -> usize {
    s.len()
}

#[derive(Debug)]
struct User {
    pub username: String,
    pub email: String,
    pub sign_in_count: u64,
    pub active: bool,
}

#[derive(Debug)]
enum IpAddrKind {
    V4,
    V6,
}

#[derive(Debug)]
enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}

fn value_of_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter => 25,
    }
}

// enum Result<T, E> {
//     Ok(T),
//     Err(E),
// }

fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err(String::from("Division by zero"))
    } else {
        Ok(a / b)
    }
}

fn get_element(index: usize, vec: &Vec<i32>) -> Option<i32> {
    if index < vec.len() {
        Some(vec[index])
    } else {
        None
    }
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

pub fn run_example() -> () {
    // 输出到命令行，可变参数当成一个数组来处理，下标从 0 开始
    let a = 12;
    println!("a is {0}, a again is {0}, {{}}", a);

    // 变量
    let b = 123; // 不可变变量
    let mut c = 10; // 可变变量

    // 函数
    c = add(b, c);
    println!("c is {}", c);

    // if 表达式
    let number = 7;
    if number < 5 {
        println!("小于 5");
    } else {
        println!("大于 5");
    }

    // loop 循环
    let mut counter = 0;
    loop {
        counter += 1;
        if counter == 10 {
            println!("counter is {}", counter);
            break;
        }
    }

    // while 循环
    let mut number = 3;
    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }

    // for 循环
    for number in 1..4 {
        println!("{}!", number);
    }

    // 所有权
    let s1 = String::from("hello");
    let s2 = s1; // s1 的所有权转移给了 s2
    //println!("{}", s1); // 此处编译会报错，因为 s1 已不再拥有该值
    println!("{}", s2);

    // 借用和引用
    let s = String::from("hello");
    let len = caculate_length(&s); // 借用
    println!("The length of '{}' is {}", s, len);

    // 结构体
    let user1 = User {
        username: String::from("someusername"),
        email: String::from("someone@example.com"),
        sign_in_count: 1,
        active: true,
    };
    println!("{:?}", user1);

    // 枚举
    let four = IpAddrKind::V4;
    let six = IpAddrKind::V6;

    println!("IPAddrKind V4 :{:?} V6 :{:?}", four, six);

    // 模式匹配
    let value = value_of_cents(Coin::Dime);
    println!("value of coin Dime is {}", value);

    // 错误处理
    println!("10 division by 2 is {:?}", divide(10, 2));
    println!("10 division by 0 is {:?}", divide(10, 0));
    // 生成 1 到 10 的 vec
    //let vec1 = vec![1, 2, 4, 5, 6];
    let vec1 = (1..10).collect::<Vec<i32>>();
    println!("index 1 of vec1 is {:?}", get_element(1, &vec1));
    println!("index 11 of vec1 is {:?}", get_element(11, &vec1));

    // 所有权与借用的生命周期
    println!("longest str is {}", longest("hello", "world"));

    // 重影
    let x = 5;
    let x = x + 1;
    let x = x * 2;
    println!("The value of x is {}", x);
    let x = "hello";
    println!("The value of x is {}", x);

    // 可变变量变化仅能发生值的变化
    // let mut s = "123";
    // s = s.len();
    // println!("The value of s is {}", s);
}
